package com.example.dormitory_system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_system.domain.enetiy.Building;
import com.example.dormitory_system.domain.dto.sysuser.BuildingDTO;
import com.example.dormitory_system.domain.vo.sysuser.BuildingListVO;
import com.example.dormitory_system.service.BuildingService;
import com.example.dormitory_system.mapper.BuildingMapper;
import com.example.dormitory_system.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author keenan
* @description 针对表【tb_building(楼栋表)】的数据库操作Service实现
* @createDate 2025-03-26 15:28:20
*/
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building>
    implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    public void importBuildings(MultipartFile file) throws IOException {
        try {
            List<Building> buildings = ExcelUtil.readBuildingsFromExcel(file.getInputStream());
            saveBatch(buildings);
        } catch (Exception e) {
            throw new RuntimeException("Failed to import buildings from Excel", e);
        }
    }

    @Override
    public int updateBuilding(BuildingDTO buildingDTO) {
        return baseMapper.updateBuildingDynamic(buildingDTO);
    }

    @Override
    public List<BuildingListVO> sysListBuilding(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return buildingMapper.selectList(new LambdaQueryWrapper<Building>()).stream().map(item ->{
            BuildingListVO buildingListVO = new BuildingListVO();
            BeanUtil.copyProperties(item, buildingListVO);
            return buildingListVO;
        }).collect(Collectors.toList());
    }


}